import {useAppStore} from "@/store/modules/app"
const appStore = useAppStore()

export const commons = {
    /**
     * toast 提示
     */
    showToast: (message: string, callback: any = null, delay: number = 1500, options = {}) => {
        uni.showToast({
            title: message,
            icon: "none",
            mask: true,
            duration: delay,
            complete: () => {
                setTimeout(() => {
                    callback && callback()
                }, delay)
            },
            ...options,
        })
    },

    /**
     * 显示 loading
     * @param title
     * @param delay
     */
    showLoading: (title = "加载中", delay = 15000) => {
        appStore.incLoadingCount()

        if(appStore.loadingCount > 1) {
            return
        }

        uni.showLoading({
            title: title,
            mask: true,
        })

        // 10秒后自动关闭
        setTimeout(() => {
            commons.hideLoading()
        }, delay)
    },

    /**
     * 隐藏 loading
     */
    hideLoading: () => {
        appStore.decLoadingCount()

        if (appStore.loadingCount <= 0) {
            uni.hideLoading()
        }
    },

    /**
     * 显示弹窗无取消
     * @param {*} message 弹窗消息
     * @param {*} callback 弹窗回调 点击确定的回调
     */
    showModelNoCancel: (message: string, callback: any) => {
        uni.showModal({
            title: "提示",
            content: message,
            showCancel: false,
            success() {
                if (callback) {
                    callback()
                }
            },
        })
    },

    /**
     * 弹出确认框
     * @param {*} message 弹框消息
     * @param {*} confirmFn 确认方法回调
     * @param {*} cancelFn 取消方法回调
     */
    showModelConfirm: (message: string, confirmFn: any, cancelFn: any) => {
        uni.showModal({
            title: "确认提示",
            content: message,
            success(res) {
                if (res.confirm) {
                    if (confirmFn) {
                        confirmFn()
                    }
                } else {
                    if (cancelFn) {
                        cancelFn()
                    }
                }
            },
        })
    },
}